﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace Economo.ExceptionHandling
{    
    public class ManageExceptions
    {
        
        public const string DataAccessPolicy = "DataAccessPolicy";
        public const string BusinessLogicPolicy = "BusinessLogicPolicy";
        public const string ServicesPolicy = "ServicesPolicy";
        public const string WinPresentationException = "UIPolicy";

        public static void ManageDataAccessException(Exception exception)
        {
            if (ExceptionPolicy.HandleException(exception, DataAccessPolicy))
                throw exception;
        }

        public static void ManageBusinessLogicException(Exception exception)
        {            
            if (ExceptionPolicy.HandleException(exception, BusinessLogicPolicy))
                throw exception;
        }
        public static void ManageServiceException(Exception exception)
        {
            if (ExceptionPolicy.HandleException(exception, ServicesPolicy))
                throw exception;
        }
        public static void ManageWinPresentationException(Exception exception)
        {
            DisplayWinPresentationException(exception);
            if (ExceptionPolicy.HandleException(exception,WinPresentationException))
                throw exception;
            
        }
        public static void DisplayWinPresentationException(Exception exception)
        {
            System.Windows.Forms.MessageBox.Show(exception.Message, "An error just happened", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
        }
        public static void LogMessage(string message)
        {
            Logger.Write(message);
        }
    }
}
